#include <QString>
#include <QtTest>

#include "test_MediaModel.h"

#include "MediaListModel.h"
#include "AbstractMediaSource.h"
#include "MockMediaSourceFactory.h"
#include "modelViewRules.h"

#define  MEDIA_EXAMPLE_PATH   "C:/Full/Path/To/Song.mp3"
#define  MEDIA_EXAMPLE_ANOTHER_PATH   "C:/Full/Path/To/AnotherSong.mp3"

test_MediaModel::test_MediaModel()
{
}

/** executed before every test case */
void test_MediaModel::init()
{
   MockMediaSourceFactory *mediaFactory = new MockMediaSourceFactory();
   m_mediaModel = new MediaListModel( mediaFactory, this);
}

/** executed after every test case */
void test_MediaModel::cleanup()
{
   delete m_mediaModel;
}

void test_MediaModel::initTestCase()
{
}

void test_MediaModel::cleanupTestCase()
{
}

void test_MediaModel::testAddOneTrack()
{
   m_mediaModel->addMediaFile( QString(MEDIA_EXAMPLE_PATH) );
   QCOMPARE(m_mediaModel->rowCount(), 1);

   QString modelPath = m_mediaModel->index(0).data( modelViewRules::Serialize).toString();
   QString modelId = m_mediaModel->index(0).data( modelViewRules::StringId).toString();

   QCOMPARE( modelPath, QString(MEDIA_EXAMPLE_PATH));
   QCOMPARE( modelId, QString("Song.mp3"));
}

/**
 * the same track is inserted twice
 */
void test_MediaModel::testRepeatedTrack()
{
   m_mediaModel->addMediaFile( QString(MEDIA_EXAMPLE_PATH) );
   m_mediaModel->addMediaFile( QString(MEDIA_EXAMPLE_PATH) );
   QCOMPARE(m_mediaModel->rowCount(), 2);

   QString stringId1st = m_mediaModel->index(0).data( modelViewRules::StringId).toString();
   QString stringId2nd = m_mediaModel->index(1).data( modelViewRules::StringId).toString();

   QCOMPARE( stringId1st, QString("Song.mp3"));
   QCOMPARE( stringId2nd, QString("Song.mp3 (2)"));
}

/**
 * removing the first item; the second becomes the first
 */
void test_MediaModel::testRemove()
{
   m_mediaModel->addMediaFile( QString(MEDIA_EXAMPLE_PATH) );
   m_mediaModel->addMediaFile( QString(MEDIA_EXAMPLE_ANOTHER_PATH) );
   QCOMPARE(m_mediaModel->rowCount(), 2);

   m_mediaModel->removeRow( 0);
   QCOMPARE(m_mediaModel->rowCount(), 1);

   QString stringId = m_mediaModel->index(0).data( modelViewRules::StringId).toString();
   QCOMPARE( stringId, QString("AnotherSong.mp3"));
}

/**
 * retrieving an item, given it's string Id
 */
void test_MediaModel::testSearchById()
{
   m_mediaModel->addMediaFile( QString(MEDIA_EXAMPLE_PATH) );
   m_mediaModel->addMediaFile( QString(MEDIA_EXAMPLE_ANOTHER_PATH) );

   QModelIndex index = m_mediaModel->searchById("AnotherSong.mp3");
   QCOMPARE( index.row(), 1);
}

/**
 * Documentative test on how to retrieve audio data.
 * In this impelmentation, there's no real audio data
 */
void test_MediaModel::testAudioData()
{
   m_mediaModel->addMediaFile( QString(MEDIA_EXAMPLE_PATH) );

   const AbstractMediaSource *mediaSource = m_mediaModel->audioData( m_mediaModel->index(0));
   QVERIFY( mediaSource->mediaContent() == QVariant());
}

